<!-- src/views/FileUploadView.vue -->
<template>
  <div class="file-upload-view">
    <h1>9.5 文件上传处理</h1>
    <p>支持拖拽、预览、进度、错误提示</p>

    <!-- 表单其他字段 -->
    <div class="form-group">
      <label>标题</label>
      <input v-model="title" type="text" placeholder="请输入标题" />
    </div>

    <!-- 文件上传组件 -->
    <FileUpload ref="uploader" accept="image/*,.pdf" :max-files="3" :max-size="10 * 1024 * 1024" />

    <div class="actions">
      <button @click="handleSubmit" :disabled="!title && !$refs.uploader?.files.length">
        提交表单
      </button>
      <button @click="handleReset" class="reset">重置</button>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import FileUpload from '@/components/FileUpload.vue'

const title = ref('')
const uploader = ref(null)

const handleSubmit = async () => {
  if (uploader.value?.files.length) {
    await uploader.value.upload()
    // 实际项目中：同时提交 title + files 到后端
    console.log('表单数据:', { title: title.value, files: uploader.value.files })
  } else {
    alert('请至少选择一个文件或填写标题')
  }
}

const handleReset = () => {
  title.value = ''
  uploader.value?.reset()
}
</script>

<style scoped>
.file-upload-view {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
}
.form-group {
  margin-bottom: 20px;
}
label {
  display: block;
  margin-bottom: 6px;
  font-weight: bold;
}
input {
  width: 100%;
  padding: 8px;
  border: 1px solid #ccc;
  border-radius: 4px;
}
.actions {
  margin-top: 20px;
}
button {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  margin-right: 12px;
}
.reset {
  background: #6c757d;
  color: white;
}
</style>
